/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.managedbean;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;
import mrp.productionPlanning.entity.MonthlyDetailEntity;
import mrp.productionPlanning.session.MonthlyDetailSessionBean;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author GKnight
 */
@ManagedBean
@ViewScoped
public class MonthlyScheduleReportManagedBean {
    
    private StreamedContent file;
    private boolean view = false;
    
    @EJB
    MonthlyDetailSessionBean monthlySessionBean;

    /** Creates a new instance of DailyScheduleReportManagedBean */
    public MonthlyScheduleReportManagedBean() {
        monthlySessionBean = new MonthlyDetailSessionBean();
    }
    
    public void exportToPDFBySQLConn(ActionEvent event){
        try {
            
            List<MonthlyDetailEntity> monthlyList;
            monthlyList = monthlySessionBean.getAllMonths();
            
            JRBeanCollectionDataSource jrResultSetDataSource = new JRBeanCollectionDataSource(monthlyList);
            
            HashMap parameters = new HashMap();
            parameters.put("IMAGEPATH", "http://localhost:8080/MerlionFoodERP-war/jasperreports/cherry.jpg");
            
            String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("jasperreports");
            String fileName = path+"/monthlySchedule.jasper";
            String outFileName = path+"/MonthlySchedule.pdf";
            // Fill the report using an empty data source
            JasperPrint print = JasperFillManager.fillReport(fileName, parameters, jrResultSetDataSource);
            
            // Create a PDF exporter
            JRExporter exporter = new JRPdfExporter();
            
            // Configure the exporter (set output file name and print object)
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            
            // Export the PDF file
            exporter.exportReport();
            
            view = true;
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Report generated successfully.", ""));
        }
        catch (JRException jrex){
            System.out.println("********** Jasperreport Exception");
            jrex.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "There is something wrong...", ""));
        }
        catch (Exception ex){
            ex.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "There is something wrong...", ""));
        }
    }
    
    public StreamedContent getFile() throws IOException {
        InputStream stream = ((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true)).getServletContext().getResourceAsStream("/jasperreports/MonthlySchedule.pdf");
        
        if (stream == null){
            System.out.println("************Cannot find file.");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The report has not been generated yet", ""));
        }
        file = new DefaultStreamedContent(stream, "application/pdf","downloadFile.pdf");
        return this.file;
    }

    /**
     * @return the view
     */
    public boolean isView() {
        return view;
    }

    /**
     * @param view the view to set
     */
    public void setView(boolean view) {
        this.view = view;
    }
}
